<?php
include_once "function_xml_basic.php";
include_once "function_base_xml_class.php";

class CResourceServiceObject extends base_xml_class {
	
	function __construct()
        {
                $this->root_path = "/MINI/RESOURCES/OBJECTS/SERVICECLASS/ATOM";
                $this->node_name = "OBJECT";
                $this->key_array = array("Name", "ObjectType", "Protocol", "IcmpType", "DstStartPort", "Type", 
					 "DstEndPort", "SrcStartPort", "SrcEndPort");
        }	
}
class CResourceServiceGroup extends base_xml_class {
	
	function __construct()
        {
                $this->root_path = "/MINI/RESOURCES/OBJECTS/SERVICECLASS/GROUPS";
                $this->node_name = "GROUP";
                $this->key_array = array("Name", "ObjectType", "ObjectList", "Type");
        }	
}
class CResourceNetObject extends base_xml_class {
	
	function __construct()
        {
                $this->root_path = "/MINI/RESOURCES/OBJECTS/IPCLASS/ATOM";
                $this->node_name = "OBJECT";
                $this->key_array = array("Name", "ObjectType", "IpType", "Ip1", "Ip2", "Type", "NetMask");
        }	
}
class CResourceNetGroup extends base_xml_class {
	
	function __construct()
        {
                $this->root_path = "/MINI/RESOURCES/OBJECTS/IPCLASS/GROUPS";
                $this->node_name = "GROUP";
                $this->key_array = array("Name", "GroupType", "ObjectList", "Type");
        }	
}

function GetObjectListByNameValue($n)
{
	$rso = new CResourceServiceObject();
	$ret_list = $rso-> get_item_by_name($n);
	//print_r($ret_list);
	//foreach ($ret_list as $ret){
	//	print_r($ret);
	//}
	return $ret_list;
}

function GetGroupListByNameValue($n)
{
	$rso = new CResourceServiceGroup();
	$ret_list = $rso-> get_item_by_name($n);
	//print_r($ret_list);
	//foreach ($ret_list as $ret){
	//	print_r($ret);
	//}
	return $ret_list;
}

function JudgeObjectOrGroupForNet($name)
{	
	$no = new CResourceNetObject();
	$list_o = $no->get_item_by_name($name);
	//print_r ($list_o);
	foreach ($list_o as $obj){
		if ($obj['Type'] == 1)
			return "OBJECT";
	}
	$ng = new CResourceNetGroup();
	$list_g = $ng->get_item_by_name($name);
	foreach($list_g as $gro){
		if ($gro['Type'] == 0)
			return "GROUP";
	}
	
	return "ERR_NO_NODE";
}

function JudgeObjectOrGroup($name)
{	
	$list_o = GetObjectListByNameValue($name);
	foreach ($list_o as $obj){
		if ($obj['Type'] == 1)
			return "OBJECT";
	}

	$list_g = GetGroupListByNameValue($name);
	foreach($list_g as $gro){
		if ($gro['Type'] == 0)
			return "GROUP";
	}
	
	return "ERR_NO_NODE";
}
function GetNetObjectAddressValue($name)
{
	$no = new CResourceNetObject();
	$ret_list = $no->get_item_by_name($name);
	foreach ($ret_list as $ret)
	{
		$array = $ret; /*array("IpType" => $ret->getAttribute("IpType"),
			       "Ip1" => $ret->getAttribute("Ip1"),
			       "Ip2" => $ret->getAttribute("Ip2"),
			       "NetMask" =>  $ret->getAttribute("NetMask"));
			      */ 
		if ($array["IpType"] == 0) //Continue
		{
			$ret_string_list["IpString"] = $array["Ip1"]. "-". $array["Ip2"];
			$ret_string_list["IpType"] = 0;
		}
		if ($array["IpType"] == 1) //Net
		{
			$ret_string_list["IpString"] = $array["Ip1"]. "/". $array["NetMask"];
			$ret_string_list["IpType"] = 1;
		}
		if ($array["IpType"] == 2) //Single
		{
			$ret_string_list["IpString"] = $array["Ip1"];
			$ret_string_list["IpType"] = 2;
		
		}
	}	
	//print_r( $ret_string_list);
	return $ret_string_list;	
}


function GetServiceGroupAddressValue($name)
{
	$ret_list = GetGroupListByNameValue($name);
	//print_r ($ret_list);
	foreach ($ret_list as $ret)
	{
		$ret_list = explode(",", $ret['ObjectList']);
	}
	//print_r ($ret_list);
	return $ret_list;
}

function GetRealNetValueByName($name)
{
	$i = 0;
	$ret_list = NULL;
	$object_type = JudgeObjectOrGroupForNet($name);
	//print $object_type. "\n";
	if ($object_type == "OBJECT")
	{
		$ret_list[$i++] = GetNetObjectAddressValue($name);
	}
	if ($object_type == "GROUP")
	{
		$ng = new CResourceNetGroup();
		$list_g = $ng->get_item_by_name($name);
		//print_r($list_g);
		foreach($list_g as $gro){
			$list = explode(",", $gro['ObjectList']);
		}
		//print_r($list);
		foreach ($list as $ret)
		{
			$ret_list[$i++] = GetNetObjectAddressValue($ret);
		}
	}
	//print_r ($ret_list);
	return $ret_list;
}

function GetServiceObjectAddressValue($name)
{
	$ret = NULL;
	$ret_list = GetObjectListByNameValue($name);
	foreach($ret_list as $s){ // Just one, only one.
		$ret = $s;
	}
	return $ret;
}
function GetRealServiceValueByName($name)
{
	$i = 0;
	$ret_list = NULL;
	$object_type = JudgeObjectOrGroup($name);

	if ($object_type == "OBJECT")
	{
		$ret_list[$i++] = GetServiceObjectAddressValue($name);
	}
	// Current config have not group :(
	if ($object_type == "GROUP")
	{
		$list = GetServiceGroupAddressValue($name);
		foreach ($list as $ret)
		{
			$ret_list[$i++] = GetServiceObjectAddressValue($ret);
		}
	}
	//print_r($ret_list);
	return $ret_list;
}

function GetTimeValue($name)
{
	return $name;
}

function GetSourceIpList($v)
{
	return GetIpList($v, "-s");
}

function GetDestIpList($v)
{
	return GetIpList($v, "-d");
}

function GetIpListForNat($value_xml, $target)
{
	$i = 0;
	$value_xml = trim($value_xml);

	if  ($value_xml == ''|| $value_xml == 'All' || $value_xml == 'nochange') {
		$dst_temp = array ("IpString" => " ", "IpType" => -1);
		$dst_ret_list = array( $dst_temp);
	}
	else if ($value_xml == "MASQUERADE"){
		$dst_temp = array("IpString" => "MASQUERADE", "IpType" => 0); 
		$dst_ret_list = array( $dst_temp);
	}	
	else {
		$dst_ret_list = GetRealNetValueByName($value_xml);
	}

	foreach ($dst_ret_list as $src_ret) {
		$srcaddr = "";
		if ($src_ret["IpString"] == NULL) {
			continue;
		}
		
		if ($src_ret["IpType"] == 1 || $src_ret["IpType"] == 2) {
			$srcaddr = " ". $target. " ".$src_ret["IpString"];
		}
		else if ($src_ret["IpType"] == 0) {
			$srcaddr = " ". $src_ret["IpString"];
		}
		else {
			$srcaddr = " ";
		}
		$ret_l [$i++] = $srcaddr;
	}
	//print_r($ret_l);
	return $ret_l;
}

function GetIpList($value_xml, $target)
{
	$i = 0;

	if  ($value_xml == ''|| $value_xml == 'All' || $value_xml == 'nochange') {
		$dst_temp = array ("IpString" => " ", "IpType" => -1);
		$dst_ret_list = array( $dst_temp);
	}
	else {
		$dst_ret_list = GetRealNetValueByName($value_xml);
	}

	foreach ($dst_ret_list as $src_ret) {
		$srcaddr = "";
		if ($src_ret["IpString"] == NULL) {
			continue;
		}
		
		if ($src_ret["IpType"] == 1 || $src_ret["IpType"] == 2) {
			$srcaddr = " ". $target. " ".$src_ret["IpString"];
		}
		else if ($src_ret["IpType"] == 0) {
			if ($target == "-s")
				$srcaddr = " -m iprange --src-range ". $src_ret["IpString"];
			if ($target == "-d")
				$srcaddr = " -m iprange --dst-range ". $src_ret["IpString"];
		}
		else {
			$srcaddr = " ";
		}
		$ret_l [$i++] = $srcaddr;
	}
	//print_r($ret_l);
	return $ret_l;
}

function GetServiceList($value_xml)
{
	$str_list = array("");
	$srvtype = "";

	if ($value_xml == '' || $value_xml == 'All')
	{
		return $str_list;
	}
	else
	{	
		$i = 0;
		$srv_ret_list = GetRealServiceValueByName($value_xml);
		//print_r($srv_ret_list);
		foreach ($srv_ret_list as $srv_ret)
		{
			$srvtype = "";
			$src_srv_port = "";
			$dst_srv_port = "";

			if ($srv_ret['Protocol'] == ''){
				continue;
			}
			else if ($srv_ret['Protocol'] == '1') {
				$srvtype = " -p icmp ";
			}
			else if ($srv_ret['Protocol'] == '6' || $srv_ret['Protocol'] == '17' ) {
				if ($srv_ret['SrcStartPort'] != "" && $srv_ret['SrcEndPort'] != "" )
				{
					if ($srv_ret['SrcStartPort'] != $srv_ret['SrcEndPort']){
						$src_srv_port .= " -m multiport --source-port ";
						$src_srv_port .= $srv_ret['SrcStartPort'];	
						$src_srv_port .= ":";
						$src_srv_port .= $srv_ret['SrcEndPort'];
						$src_srv_port .= " ";
					}
					else{
						$src_srv_port .= " -m multiport --source-port ";
						$src_srv_port .= $srv_ret['SrcStartPort'];	
						//$src_srv_port .= ":";
						//$src_srv_port .= $srv_ret['SrcEndPort'];
						$src_srv_port .= " ";
					}
				}
				if (($srv_ret['SrcStartPort'] == "" && $srv_ret['SrcEndPort'] != "") ||
				    ($srv_ret['SrcStartPort'] != "" && $srv_ret['SrcEndPort'] == ""))
				{
					$src_srv_port .= " -m multiport --source-port ";
					$src_srv_port .= $srv_ret['SrcStartPort'];	
					//$src_srv_port .= ":";
					$src_srv_port .= $srv_ret['SrcEndPort'];
					$src_srv_port .= " ";
				}
				if ($srv_ret['DstStartPort'] != "" && $srv_ret['DstEndPort'] != "" )
				{
					if ($srv_ret['DstStartPort'] != $srv_ret['DstEndPort']){
						$dst_srv_port .= " -m multiport --destination-port  ";
						$dst_srv_port .= $srv_ret['DstStartPort'];	
						$dst_srv_port .= ":";
						$dst_srv_port .= $srv_ret['DstEndPort'];
						$dst_srv_port .= " ";
					}
					else{
						$dst_srv_port .= " -m multiport --destination-port  ";
						$dst_srv_port .= $srv_ret['DstStartPort'];	
						//$dst_srv_port .= ":";
						//$dst_srv_port .= $srv_ret['DstEndPort'];
						$dst_srv_port .= " ";
					}
				}
				if (($srv_ret['DstStartPort'] == "" && $srv_ret['DstEndPort'] != "") ||
				    ($srv_ret['DstStartPort'] != "" && $srv_ret['DstEndPort'] == ""))
				{
					$dst_srv_port .= " -m multiport --destination-port  ";
					$dst_srv_port .= $srv_ret['DstStartPort'];	
					//$dst_srv_port .= ":";
					$dst_srv_port .= $srv_ret['DstEndPort'];
					$dst_srv_port .= " ";
				}
				if ($srv_ret['Protocol'] == '6')
				{
					$srvtype = " -p tcp ";
				}
				if ($srv_ret['Protocol'] == '17')
				{
					$srvtype = " -p udp ";
				}
				$srvtype .= $src_srv_port. $dst_srv_port;
			}
			else // Other integer !
			{
				$srvtype = " -p ". $srv_ret['Protocol']. " ";
			}
			$str_list[$i++] = $srvtype;
		}
	}
	//print_r($str_list);
	return $str_list;
}

include_once "function_obj_time.php";
function GetTimeString($value_xml){
	if ($value_xml == "All" || $value_xml == "")
		return "";
	
	$root_path = "/MINI/RESOURCES/OBJECTS/TIMECLASS";
	$query_string = $root_path. "/ATOM/OBJECT";
	$key_array = array('Name', 'ObjectType', 'TimeType', 
			   'StartTime', 'EndTime', 'Week');
	$obj_list = GetAttributList($query_string, $key_array);
	foreach ($obj_list as $obj)
        {   
                if ($obj['Name'] == $value_xml){
                        $obj_time = $obj;
			break;
		}	
        }

	$ret_com = " -m time ";
	if ($obj_time['TimeType'] == 1 ){ // circle
		$ret_com .= " --weekdays ". $obj_time['Week'];
		$ret_com .= (trim($obj_time['StartTime']) != "" )?(" --timestart ". $obj_time['StartTime']. " "):("");
		$ret_com .= (trim($obj_time['EndTime']) != "")?(" --timestop ". $obj_time['EndTime']. " "):("");
	}
	else if ($obj_time['TimeType'] == 2){ // single
		$ret_com .= (trim($obj_time['StartTime']) != "")?(" --datestart ". $obj_time['StartTime']." "):(""); 
		$ret_com .= (trim($obj_time['EndTime']) != "")?(" --datestop ". $obj_time['EndTime']. " "):("");
	}

	return $ret_com;
}
?>
